import 'package:flutter/material.dart';
import 'package:gold/base/provider_scaffold.dart';
import 'package:gold/ui/my/integral/integral_detail_viewmodel.dart';
import 'package:gold/utils/res.dart';
import 'package:gold/utils/xfg_color.dart';
import 'package:gold/utils/xfg_size.dart';
import 'package:gold/widgets/common_widgets.dart';
import 'package:gold/widgets/dialog/remain_points_description_dialog.dart';
import 'package:gold/widgets/xfg_persistent_header_delegate.dart';
import 'package:gold/widgets/xfg_text.dart';
import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';

/// @description: 积分明细
/// @author tangzhentao
/// @created at 21:32 2024/12/10

class IntegralDetailPage extends StatelessWidget {
  const IntegralDetailPage({super.key});

  @override
  Widget build(BuildContext context) {
    return ProviderScaffold<IntegralDetailViewmodel>(
        appBar: (_) => xfgAppbar(title: '积分明细'),
        backgroundColor: XfgColor.white,
        model: IntegralDetailViewmodel(),
        onInit: (data) => data.loadFirst(),
        builder: (ctx, detail) {
          final statistics = detail.statistics;
          final list = detail.list;
          return SmartRefresher(
            controller: detail.controller,
            enablePullDown: true,
            enablePullUp: true,
            onRefresh: detail.refresh,
            onLoading: detail.loadMore,
            child: CustomScrollView(
              slivers: [
                /// 明细资料
                SliverToBoxAdapter(
                  child: Container(
                    margin: EdgeInsets.symmetric(vertical: XfgSize().size20),
                    alignment: Alignment.center,
                    child: Container(
                      height: XfgSize().size250,
                      width: XfgSize().size355,
                      decoration: const BoxDecoration(image: DecorationImage(image: AssetImage(Res.imagesIntegralDetailTopBg), fit: BoxFit.cover)),
                      child: Column(
                        children: [
                          SizedBox(height: XfgSize().size14),
                          Expanded(
                              child: Column(
                                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                children: [
                                  Row(
                                    children: [
                                      Expanded(
                                        child: Container(
                                          margin: EdgeInsets.only(left: XfgSize().size26),
                                          height: XfgSize().size22,
                                          child: Row(
                                            children: [
                                              XfgText('剩余积分 ', fontSize: XfgSize().text16, color: XfgColor.white),
                                              GestureDetector(
                                                onTap: () => showRemainPointsDescriptionDialog(statistics.remainingPoints?? 0, statistics.incomingPoints?? 0),
                                                child: Image.asset(Res.imagesIntegralDetailDescription, width: XfgSize().size22, height: XfgSize().size22),
                                              )
                                            ],
                                          ),
                                        ),
                                      ),
                                      Expanded(
                                        child: Container(
                                          margin: EdgeInsets.only(left: XfgSize().size26),
                                          height: XfgSize().size22,
                                          child: Row(children: [XfgText('今日预计到账', fontSize: XfgSize().text16, color: XfgColor.integralExpected)]),
                                        ),
                                      )
                                    ],
                                  ),
                                  Row(
                                    crossAxisAlignment: CrossAxisAlignment.start,
                                    children: [
                                      SizedBox(width: XfgSize().size26),
                                      Expanded(child: XfgText(statistics.remainingPoints?.toString()?? '0', fontSize: XfgSize().text32, color: XfgColor.white, bold: FontWeight.bold, maxLines: 1, overflow: TextOverflow.ellipsis)),
                                      SizedBox(width: XfgSize().size26),
                                      Expanded(child: XfgText(statistics.incomingPoints?.toString()?? '0', fontSize: XfgSize().text24, color: XfgColor.white, bold: FontWeight.w600, maxLines: 1, overflow: TextOverflow.ellipsis)),
                                    ],
                                  ),
                                  Container(
                                    width: XfgSize().size300,
                                    height: XfgSize().size24,
                                    padding: EdgeInsets.symmetric(horizontal: XfgSize().size12),
                                    decoration: BoxDecoration(color: XfgColor.integralDetailTipBg, borderRadius: BorderRadius.circular(XfgSize().size24)),
                                    child: Row(
                                      children: [
                                        Icon(Icons.alarm_on_outlined, size: XfgSize().size16, color: XfgColor.integralDetailTip),
                                        XfgText('  确认收货后积分才能入账', fontSize: XfgSize().text12, color: XfgColor.white)
                                      ],
                                    ),
                                  ),
                                ],
                              )
                          ),
                          SizedBox(
                            height: XfgSize().size134,
                            child: Column(
                              children: [
                                Expanded(
                                  child: Row(
                                    children: [
                                      _dataItemWidget1('累计积分', statistics.totalPoints?.toString()?? '0'),
                                      _dataItemWidget1('已分配', statistics.allocatedPoints?.toString()?? '0'),
                                      _dataItemWidget1('待分配', statistics.pendingPoints?.toString()?? '0'),
                                    ],
                                  ),
                                ),
                                Expanded(
                                  child: Row(
                                    children: [
                                      _dataItemWidget2('累计赠与', statistics.totalGiftedPoints?.toString()?? '0'),
                                      _dataItemWidget2('累计抵扣', statistics.totalRedeemedPoints?.toString()?? '0'),
                                      const Spacer()
                                    ],
                                  ),
                                )
                              ],
                            ),
                          )
                        ],
                      ),
                    ),
                  ),
                ),
                /// 变动明细标题
                SliverPersistentHeader(
                  pinned: true,
                  floating: true,
                  delegate: XfgPersistentHeaderDelegate(
                      maxHeight: XfgSize().size54,
                      minHeight: XfgSize().size54,
                      child: Container(
                        color: XfgColor.white,
                        padding: EdgeInsets.symmetric(horizontal: XfgSize().size10),
                        child: Container(
                          padding: EdgeInsets.only(bottom: XfgSize().size14, top: XfgSize().size20),
                          decoration: bottomBorderDecoration,
                          child: Row(
                            children: [
                              Image.asset(Res.imagesIntegralDetail, width: XfgSize().size20, height: XfgSize().size20),
                              SizedBox(width: XfgSize().size6),
                              XfgText('变动明细', fontSize: XfgSize().text20, color: XfgColor.text1D, bold: FontWeight.bold)
                            ],
                          ),
                        ),
                      )
                  ),
                ),
                SliverList(
                  delegate: SliverChildBuilderDelegate(
                          (_, index) {
                        final model = list[index];
                        final points = model.points?? 0;
                        final isPlus = points >= 0;
                        String value = points.toString();
                        if (points > 0) {
                          value = '+$points';
                        }
                        return Container(
                          margin: EdgeInsets.symmetric(horizontal: XfgSize().size10),
                          padding: EdgeInsets.symmetric(vertical: XfgSize().size16),
                          decoration: bottomBorderDecoration,
                          child: Row(
                            children: [
                              Expanded(child:  XfgText('${model.reason?? ''}\n', fontSize: XfgSize().text18, color: XfgColor.text1D, bold: FontWeight.bold, height: 2, children: [
                                TextSpan(text: '订单: ${model.orderId?.toString()?? '--'}\n', style: TextStyle(fontSize: XfgSize().text16, color: XfgColor.text1, fontWeight: FontWeight.normal)),
                                TextSpan(text: model.createdAt?? '', style: TextStyle(fontSize: XfgSize().text14, color: XfgColor.text3, fontWeight: FontWeight.normal)),
                              ])),
                              XfgText(value, fontSize: XfgSize().text20, color: isPlus ? XfgColor.integralPlus:XfgColor.text1D, bold: FontWeight.w600)
                            ],
                          ),
                        );
                      },
                      childCount: list.length
                  ),
                ),
              ],
            ),
          );
        }
    );
  }

  /// 数据通用布局1
  Widget _dataItemWidget1(String text, String value) => Expanded(
    child: Column(
      mainAxisAlignment: MainAxisAlignment.end,
      children: [
        XfgText(text, fontSize: XfgSize().text14, color: XfgColor.integralExpected),
        SizedBox(height: XfgSize().size10),
        XfgText(value, fontSize: XfgSize().text16, color: XfgColor.white),
        SizedBox(height: XfgSize().size12),
      ],
    ),
  );

  /// 数据通用布局2
  Widget _dataItemWidget2(String text, String value) => Expanded(
    child: Column(
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        SizedBox(height: XfgSize().size12),
        XfgText(text, fontSize: XfgSize().text14, color: XfgColor.integralExpected),
        SizedBox(height: XfgSize().size10),
        XfgText(value, fontSize: XfgSize().text16, color: XfgColor.white),
      ],
    ),
  );
}
